﻿using MVCDemo.DAL;
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers
{
    public class AccountController : Controller
    {
        private AccountContext db = new AccountContext();
        // GET: Account
        public ActionResult Index()
        {
            return View(db.SysUsers);
        }

        public ActionResult Details(int id)
        {
            var user = db.SysUsers.Find(id);
            return View(user);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(SysUser sysUser)
        {
            db.SysUsers.Add(sysUser);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id=0)
        {
            SysUser user = db.SysUsers.Find(id);
            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(SysUser sysUser)
        {
            db.Entry(sysUser).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            SysUser syuser = db.SysUsers.Find(id);
            return View(syuser);
        }

        [HttpPost,ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            SysUser sysUser = db.SysUsers.Find(id);
            db.SysUsers.Remove(sysUser);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        public ActionResult Login()
        {
            ViewBag.LoginState = "未登录...";
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection fc)
        {
            //获取表单数据
            string email = fc["femail"];
            string pass = fc["fpass"];

            //进行处理
            //ViewBag.LoginState = "用户 ["+email + "] 进入系统";

            var user = db.SysUsers.Where(x => x.Email == email && x.Password == pass);
            ViewBag.LoginState = user.Count() > 0 ? email + "进入系统" : "用户名或密码错误";

            //linq 查询用linq实现
            var users = from u in db.SysUsers select u; //表达式方式
            users = db.SysUsers;  //函数式方式, 简单，推荐

            //查询
            users = from u in db.SysUsers
                    where u.UserName == "Tom"
                    select u;
            users = db.SysUsers.Where(x => x.UserName == "Tom");

            //排序 分页
            users = (from u in db.SysUsers
                     orderby u.UserName
                     select u).Skip(0).Take(5);
            users = db.SysUsers.OrderBy(u => u.UserName).Skip(0).Take(5);

            //聚合
            var num = db.SysUsers.Count();
            var minid = db.SysUsers.Min(x => x.ID);

            //链接查询 返回的还是SysUserRoles的集合，经过了内联的筛选
            var _users = from ur in db.SysUserRoles
                        join u in db.SysUsers
                        on ur.SysUserID equals u.ID
                        select ur;


            return View();
        }

        //EF 数据更新
        public ActionResult EDUpdateDemo()
        {
            var sysUser = db.SysUsers.FirstOrDefault(x => x.UserName == "Tom");
            if (sysUser != null)
            {
                sysUser.UserName = "Tom2";
            }
            db.SaveChanges();

            return View();
        }

        //EF添加 删除 数据
        public ActionResult EFAddOrDeleteDemo()
        {
            var newUser = new SysUser()
            {
                UserName = "Liyy",
                Password = "3",
                Email = "300@qq.com"
            };
            db.SysUsers.Add(newUser);
            db.SaveChanges();

            //删除
            var delUser = db.SysUsers.FirstOrDefault(x => x.UserName == "Scott");
            if (delUser != null)
            {
                db.SysUsers.Remove(delUser);
            }
            db.SaveChanges();

            return View("EFQueryDemo");
        }



        public ActionResult Register()
        {
            return View();
        }


    }
}